using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RideMe.Core;
using RideMe.Presentation.Properties;

namespace RideMe.Presentation
{
    public partial class SnippetManagerDialog : RideMe.Presentation.DialogBase
    {
        public SnippetManagerDialog()
        {
            InitializeComponent();
            LoadSnippets();
        }

        private DialogEditMode _dialogEditMode = DialogEditMode.New;
        private List<Snippet> _snippets;
        private bool _dirty;
        private bool _cancel;

        private void LoadSnippets()
        {
            _snippets = SnippetService.GetSnippets();

            foreach (Snippet snippet in _snippets)
            {
                AddSnippetListItem(snippet);
            }
        }

        private void AddSnippetListItem(Snippet snippet)
        {
            ListViewItem item = new ListViewItem(snippet.Name);
            item.SubItems.Add(snippet.Shortcut);
            item.SubItems.Add(snippet.SnippetValue);
            item.Tag = snippet;
            snippetsListView.Items.Add(item);
        }

        private void EditSnippet(Snippet snippet)
        {
            _dialogEditMode = DialogEditMode.Edit;
            nameTextBox.Text = snippet.Name;
            shortcutTextBox.Text = snippet.Shortcut;
            snippetTextBox.Text = snippet.SnippetValue;
            UpdateSaveButton(Resources.UpdateButtonCaption);
        }

        private void ClearSnippetEditors()
        {
            nameTextBox.Text = string.Empty;
            shortcutTextBox.Text = string.Empty;
            snippetTextBox.Text = string.Empty;
        }

        private Snippet UpdateSnippet(Snippet snippet)
        {
            snippet.Name = nameTextBox.Text;
            snippet.Shortcut = shortcutTextBox.Text;
            snippet.SnippetValue = snippetTextBox.Text;

            return snippet;
        }

        private void UpdateDisplay(Snippet snippet)
        {
            snippetsListView.SelectedItems[0].Text = snippet.Name;
            snippetsListView.SelectedItems[0].SubItems[1].Text = snippet.Shortcut;
            snippetsListView.SelectedItems[0].SubItems[2].Text = snippet.SnippetValue;
            snippetsListView.Tag = snippet;
        }

        private void UpdateSaveButton(string buttonText)
        {
            saveButton.Text = buttonText;
        }

        private bool SaveNewSnippet()
        {
            Snippet snippet = new Snippet();
            snippet = UpdateSnippet(snippet);
            if (ValidSnippet(snippet))
            {
                _snippets.Add(snippet);
                AddSnippetListItem(snippet);
                ClearSnippetEditors();
                return true;
            }
            else
            {
                MessageService.ShowError(this, Resources.InvalidSnippetMessage);
                return false;
            }
        }

        private bool SaveEditedSnippet()
        {
            Snippet snippet = UpdateSnippet(snippetsListView.SelectedItems[0].Tag as Snippet);
            if (ValidSnippet(snippet))
            {
                UpdateDisplay(snippet);
                return true;
            }
            else
            {
                MessageService.ShowError(this, Resources.InvalidSnippetMessage);
                return false;
            }
        }

        private bool ValidSnippet(Snippet snippet)
        {
            bool valid = true;

            if (!Validator.ValidString(snippet.Name))
            {
                valid = false;
            }
            else if (!Validator.ValidString(snippet.Shortcut))
            {
                valid = false;
            }

            return valid;
        }

        private void snippetsListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (snippetsListView.SelectedItems.Count > 0)
            {
                EditSnippet(((Snippet)snippetsListView.SelectedItems[0].Tag));
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            _dialogEditMode = DialogEditMode.New;
            ClearSnippetEditors();
            nameTextBox.Focus();
            UpdateSaveButton(Resources.AddButtonCaption);
        }


        private void saveButton_Click(object sender, EventArgs e)
        {
            if (_dialogEditMode == DialogEditMode.Edit)
            {
                _dirty = SaveEditedSnippet();
            }
            else
            {
                _dirty = SaveNewSnippet();
            }

            if (_dirty)
            {
                okButton.Enabled = true;
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (snippetsListView.SelectedItems.Count > 0)
            {
                if (MessageService.ShowQuestion(this, Resources.DeleteSnippetConfirmation) == DialogResult.Yes)
                {
                    _snippets.Remove(snippetsListView.SelectedItems[0].Tag as Snippet);
                    snippetsListView.Items.Remove(snippetsListView.SelectedItems[0]);
                    ClearSnippetEditors();
                    _dialogEditMode = DialogEditMode.New;
                    _dirty = true;
                    okButton.Enabled = true;
                }
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            SnippetService.SaveSnippets(_snippets);
            _dirty = false;
            this.Close();
        }

        private void SnippetManagerDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((_dirty) && (!_cancel))
            {
                if (MessageService.ShowQuestion(this, Resources.SaveSnippetsMessage) == DialogResult.Yes)
                {
                    SnippetService.SaveSnippets(_snippets);
                }
            }
        }

        private void snippetsListView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (snippetsListView.Sorting == SortOrder.Descending)
            {
                snippetsListView.Sorting = SortOrder.Ascending;
            }
            else
            {
                snippetsListView.Sorting = SortOrder.Descending;
            }

            snippetsListView.Sort();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            _cancel = true;
        }

    }
}

